﻿using System;
using System.Net;
using System.Windows;
using SuperLauncher.Entitys;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Windows.Resources;
using Microsoft.Silverlight.Windows.Taskbar;

namespace SuperLauncher.Helpers
{
    public static class DataHelper
    {
        public const string NoCategoryName = "未分类";
        public static string XmlDataPath
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SuperLauncherData.xml");
            }
        }

        public static System.Collections.ObjectModel.ObservableCollection<CategoryInfo> CategoryInfos
        {
            get;
            set;
        }

        public static System.Collections.ObjectModel.ObservableCollection<CategoryInfo> ReadData()
        {
            System.Collections.ObjectModel.ObservableCollection<CategoryInfo> categoryInfos = new System.Collections.ObjectModel.ObservableCollection<CategoryInfo>();

            if (File.Exists(DataHelper.XmlDataPath))
            {
                FileStream file = File.OpenRead(DataHelper.XmlDataPath);
                XDocument xdoc = XDocument.Load(file);

                foreach (XElement node in xdoc.Root.Nodes())
                {
                    CategoryInfo category = new CategoryInfo()
                    {
                        Title = node.Attribute("Title").Value
                    };

                    foreach (XElement itemNode in node.Nodes())
                    {
                        ItemInfo item = new ItemInfo()
                        {
                            CommandLineArguments = itemNode.Attribute("CommandLineArguments").Value,
                            Description = itemNode.Attribute("Description").Value,
                            Path = itemNode.Attribute("Path").Value,
                            Title = itemNode.Attribute("Title").Value,
                            StartDirectory = itemNode.Attribute("StartDirectory").Value
                        };

                        try
                        {
                            item.LaunchingItemID = Int32.Parse(itemNode.Attribute("LaunchingItemID").Value);
                        }
                        catch { }

                        category.Items.Add(item);
                    }

                    categoryInfos.Add(category);
                }

                file.Close();
            }

            // DataHelper.WatchChange(categoryInfos);
            return categoryInfos;
        }

        //public static void WatchChange(System.Collections.ObjectModel.ObservableCollection<CategoryInfo> categoryInfos)
        //{
        //    categoryInfos.CollectionChanged += (a, b) =>
        //    {
        //        DataHelper.SetOverlayIconSave();
        //    };

        //    foreach (var category in categoryInfos)
        //    {
        //        category.PropertyChanged += (a, b) =>
        //        {
        //            DataHelper.SetOverlayIconSave();
        //        };

        //        category.Items.CollectionChanged += (a, b) =>
        //        {
        //            DataHelper.SetOverlayIconSave();
        //        };

        //        foreach (var item in category.Items)
        //        {
        //            item.PropertyChanged += (a, b) =>
        //            {
        //                DataHelper.SetOverlayIconSave();
        //            };
        //        }
        //    }
        //}

        public static void SaveData(System.Collections.ObjectModel.ObservableCollection<CategoryInfo> categoryInfos)
        {
            XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
            XElement root = new XElement("Categorys");
            xdoc.Add(root);

            int i = 0;
            if (categoryInfos != null)
            {
                foreach (var category in categoryInfos)
                {
                    XElement xcategory = new XElement("Category", new XAttribute("Title", category.Title));
                    root.Add(xcategory);

                    foreach (var item in category.Items)
                    {
                        item.LaunchingItemID = i;
                        XElement xitem = new XElement("Item",
                            new XAttribute("CommandLineArguments", item.CommandLineArguments),
                            new XAttribute("Description", item.Description),
                            new XAttribute("Path", item.Path),
                            new XAttribute("LaunchingItemID", item.LaunchingItemID),
                            new XAttribute("Title", item.Title),
                            new XAttribute("StartDirectory", item.StartDirectory));

                        xcategory.Add(xitem);
                        i++;
                    }
                }
            }

            StreamWriter sw = new StreamWriter(DataHelper.XmlDataPath, false, System.Text.Encoding.UTF8);
            xdoc.Save(sw, SaveOptions.None);
            sw.Flush();
            sw.Close();
        }

        public static void SetOverlayIconSave()
        {
            StreamResourceInfo sri = Application.GetResourceStream(new Uri("/SuperLauncher;component/Assets/disk.png", UriKind.Relative));
            //open a reader
            using (BinaryReader br = new BinaryReader(sri.Stream))
            {
                //set the overlay icon
                TaskbarButton.Current.SetOverlayIcon(
                  br.ReadBytes((int)sri.Stream.Length), //PNG file content
                  "In Edit Mode", //description
                  ButtonImageDataType.PNG //image data type
                  );
            }
        }

        public static void SetOverlayIconAccept()
        {
            StreamResourceInfo sri = Application.GetResourceStream(new Uri("/SuperLauncher;component/Assets/accept.png", UriKind.Relative));
            //open a reader
            using (BinaryReader br = new BinaryReader(sri.Stream))
            {
                //set the overlay icon
                TaskbarButton.Current.SetOverlayIcon(
                  br.ReadBytes((int)sri.Stream.Length), //PNG file content
                  "In Edit Mode", //description
                  ButtonImageDataType.PNG //image data type
                  );
            }
        }

        public static void SetOverlayIconEmpty()
        {
            TaskbarButton.Current.SetOverlayIcon(
               null,
              string.Empty,
              ButtonImageDataType.PNG); 
        }
    }
}
